import UIKit

@objc class LaunchViewController: UIViewController {
    
    @objc var onAnimationComplete: (() -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        startAnimation()
    }
    
    private func setupUI() {
        // 背景图片（全屏）
        let backgroundImage = UIImageView()
        backgroundImage.image = UIImage(named: "launchbg")
        backgroundImage.contentMode = .scaleAspectFill  // 保持比例填充
        backgroundImage.clipsToBounds = true
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(backgroundImage)
        
        let logoImage = UIImageView()
        logoImage.image = UIImage(named: "logo")
        logoImage.frame=CGRect(x:0,y:0,width:140,height:120)
        logoImage.contentMode = .scaleAspectFill  // 保持比例填充
        logoImage.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(logoImage)
        
        // 文字标签
        let titleLabel = UILabel()
        titleLabel.text = NSLocalizedString("launch_title", comment: "Launch screen title")
        titleLabel.font = .boldSystemFont(ofSize: 16)
        titleLabel.alpha = 0  // 初始透明
      titleLabel.textColor = UIColor(
          red: 140.0/255.0,
          green: 140.0/255.0,
          blue: 140.0/255.0,
          alpha: 1.0
      )

        titleLabel.textAlignment = .center
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(titleLabel)
        
        // 布局约束
        NSLayoutConstraint.activate([
            // 背景图全屏
            backgroundImage.topAnchor.constraint(equalTo: view.topAnchor),
            backgroundImage.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            backgroundImage.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            backgroundImage.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            
            // logo 居中，距离顶部约1/4屏幕高度
            logoImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            logoImage.topAnchor.constraint(equalTo: view.topAnchor, constant: UIScreen.main.bounds.height * 0.25),
            logoImage.widthAnchor.constraint(equalToConstant: 100),
            logoImage.heightAnchor.constraint(equalToConstant: 100),
            
      
            
            // 文字居中，位于logo下方
            titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            titleLabel.topAnchor.constraint(equalTo: logoImage.bottomAnchor, constant: 8),
            titleLabel.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -20)
        ])
    }
    
    private func startAnimation() {
        // 淡入动画
        UIView.animate(withDuration: 1.5, delay: 0.3, options: .curveEaseIn, animations: {
            self.view.subviews.compactMap { $0 as? UILabel }.first?.alpha = 1
        }) { _ in
          DispatchQueue.main.asyncAfter(deadline: .now()) {
                self.onAnimationComplete?()
            }
        }
        
        // 可选：添加缩放动画
        if let label = self.view.subviews.compactMap({ $0 as? UILabel }).first {
            label.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
            UIView.animate(withDuration: 1.8, delay: 0.3, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: {
                label.transform = .identity
            })
        }

        // 添加logo动画
        if let logo = self.view.subviews.compactMap({ $0 as? UIImageView }).first(where: { $0.image == UIImage(named: "logo") }) {
            logo.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
            UIView.animate(withDuration: 1.5, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: [], animations: {
                logo.transform = .identity
            })
        }
    }
}
